Page({
  data: {
    activityInfo: {
      
    }
  },

  onLoad(options) {
    console.log(options)
    // 根据id获取活动详情
    if (options.aid) {
      this.getActivityDetail(options.aid)
    }
  },

  getActivityDetail(id) {
    // TODO: 调用接口获取活动详情
    const app = getApp()
    app.activityApi.selectById(id).then(
      res => {
        this.setData({
          activityInfo: res.data.data
        })
        console.log(res.data)
      }
    )
  },

  previewImage(e) {
    const currentUrl = e.currentTarget.dataset.url; // 获取当前点击的图片 URL
    const urls = []; // 存储所有图片的 URL

    // 将所有图片的 URL 添加到数组中
    if (this.data.activityInfo.imagePath1) {
      urls.push(this.data.activityInfo.imagePath1);
    }
    if (this.data.activityInfo.imagePath2) {
      urls.push(this.data.activityInfo.imagePath2);
    }
    if (this.data.activityInfo.imagePath3) {
      urls.push(this.data.activityInfo.imagePath3);
    }
    if (this.data.activityInfo.imagePath4) {
      urls.push(this.data.activityInfo.imagePath4);
    }

    // 使用 wx.getImageInfo 预加载图片
    const promises = urls.map(url => {
      return new Promise((resolve, reject) => {
        wx.getImageInfo({
          src: url,
          success: resolve,
          fail: reject
        });
      });
    });

    Promise.all(promises).then(() => {
      // 调用 wx.previewImage 显示预览
      wx.previewImage({
        current: currentUrl, // 当前显示图片的http链接
        urls: urls // 需要预览的图片http链接列表
      });
    }).catch(err => {
      console.error('获取图片信息失败:', err);
    });
  },

  onReport(event) {
    const aid = event.currentTarget.dataset.aid
    wx.navigateTo({
      url: '/pages/activity/joinAddition?aid='+aid
    })
  },

  onCancel() {
    wx.showModal({
      title: '提示',
      content: '确定要取消该活动吗？',
      success: (res) => {
        if (res.confirm) {
          const app = getApp();
          const {
            aid,
            status
          } = this.data.activityInfo;

          // 显示加载中提示（可选）
          wx.showLoading({
            title: '正在删除...'
          });
          app.activityApi.deleteActivity({
              aid,
              status
            })
            .then(() => {
              // 关闭加载提示
              wx.hideLoading();
              // 显示成功 Toast
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000 // 确保设置为 2000 毫秒
              });
              // 明确使用 setTimeout 延时跳转
              setTimeout(() => {
                wx.navigateBack();
              }, 2000);
            })
            .catch(err => {
              wx.hideLoading();
              wx.showToast({
                title: '删除失败',
                icon: 'error'
              });
            });
        }
      }
    });
  },
  onPullDownRefresh() {
    const id = this.data.activityInfo.aid
    this.getActivityDetail(id)
    setTimeout(() => {
      wx.stopPullDownRefresh()
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 1000
      })
    }, 1000)
  }
})